package de.preiscrawler.parser;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import net.htmlparser.jericho.Element;
import net.htmlparser.jericho.Source;
import de.preisrobot.entity.Artikel;
import de.preisrobot.service.internal.PreisRobotParserException;

public class AmazonParser {

	 
	@SuppressWarnings("deprecation")
	public List<Artikel> parse(String sourceUrlString) throws Exception {

		

		if (sourceUrlString == null || sourceUrlString.length() == 0)
			throw new RuntimeException("Using default argument of \""
					+ sourceUrlString + '"');

		Source source = new Source(new URL(sourceUrlString));

		return parse(source);
	}

	public List<Artikel> parse(Source source) throws Exception {
		
		List<Artikel> list = new ArrayList<Artikel>();
		
		Element elementById = source.getElementById("btfResults");

		if (elementById == null)
			throw new PreisRobotParserException();

		List<Element> elementList = elementById
				.getAllElementsByClass("result product");

		for (Element element : elementList) {

			Artikel artikel = new Artikel();

			String itemName = element.getAttributeValue("name");
			artikel.setArtikelNr(itemName);

			List<Element> priceTag = element.getAllElementsByClass("newPrice");
			List<Element> titles = element
					.getAllElementsByClass("productTitle");

			if (priceTag != null && !priceTag.isEmpty())
				getPrice(priceTag.get(0), artikel);

			if (titles != null && !titles.isEmpty())
				getProductLine(titles.get(0), artikel);

			list.add(artikel);
		}

		return list;
	}

	private void getPrice(Element element, Artikel artikel) {
		element.getFirstElement();
		List<Element> allElements = element.getAllElements();
		String[] arr = new String[3];

		for (Element element2 : allElements) {
			String name = element2.getName();

			if (name.contains("span")) {
				// Sring imagePath =getImage(element2);
				String attributeValue = element2.getTextExtractor().toString();
				arr[0] = attributeValue;
			} else if (name.startsWith("a")) {
				String attributeValue = element2.getAttributeValue("href");
				String attributeValue2 = element2.getTextExtractor().toString();
				arr[1] = attributeValue;
				arr[2] = attributeValue2;
			}

		}

		artikel.setPreis(arr[0]);
		artikel.setArtikelPreisRef(arr[1]);
		artikel.setPreisDesc(arr[2]);
	}

	private void getProductLine(Element element, Artikel artikel) {

		Element firstElement = element.getFirstElement();
		List<Element> allElements = firstElement.getAllElements();
		String[] arr = new String[3];

		for (Element element2 : allElements) {
			String name = element2.getName();

			if (name.contains("img")) {
				// Sring imagePath =getImage(element2);
				String attributeValue = element2.getAttributeValue("src");
				arr[0] = attributeValue;
			} else if (name.startsWith("a")) {
				String attributeValue = element2.getAttributeValue("href");
				String attributeValue2 = element2.getTextExtractor().toString();
				arr[1] = attributeValue;
				arr[2] = attributeValue2;
			}

		}
		artikel.setImageLink(arr[0]);
		artikel.setArtikelLink(arr[1]);
		artikel.setDesc(arr[2]);
		System.out.println(firstElement.getName() + " " + arr[0] + " " + arr[1]
				+ " " + arr[2]);
	}
}
